*Joining couples*
clear

use "Data\dataset_with_singles.dta" 

*merge on final_educ_year from sample1_new
merge 1:1 pnr aar using "Data\sample1_new.dta", keepusing(final_educ_year)

drop if _merge==2
drop _merge


gen ambition=.
replace ambition=1 if ambition_type_k_4_s==1
replace ambition=2 if ambition_type_k_4_s==4
replace ambition=3 if ambition_type_k_4_s==3
replace ambition=4 if ambition_type_k_4_s==2

drop ambition_type_k_4_s

sort pnr aar
by pnr: egen first_year=min(aar)
drop individual
gen individual=1 if aar==first_year
drop first_year

gen wage_start_mean_ambition_s_all=wage_start_mean_ambition_s
drop wage_start_mean_ambition_s
gen wage_growth_ambition_s_all=wage_growth_ambition_s
drop wage_growth_ambition_s

*Merge on first decade

merge 1:1 pnr aar using "Results\tab_B3\Scenario_3\sample2_first_decade.dta", keepusing(ambition_type_k_4_s wage_start_mean_ambition_s wage_growth_ambition_s)

drop if _merge==2
drop _merge

gen ambition_type_sep=. if !missing(ambition_type_k_4_s)
replace ambition_type_sep=1 if ambition_type_k_4_s==2 & final_educ_year<=1989
replace ambition_type_sep=2 if ambition_type_k_4_s==1 & final_educ_year<=1989
replace ambition_type_sep=3 if ambition_type_k_4_s==4 & final_educ_year<=1989
replace ambition_type_sep=4 if ambition_type_k_4_s==3 & final_educ_year<=1989



drop ambition_type_k_4_s

gen wage_start_mean_ambition_s_first=wage_start_mean_ambition_s
drop wage_start_mean_ambition_s
gen wage_growth_ambition_s_first=wage_growth_ambition_s
drop wage_growth_ambition_s

*Merge on second decade

merge 1:1 pnr aar using "Results\tab_B3\Scenario_3\sample2_second_decade.dta", keepusing(ambition_type_k_4_s wage_start_mean_ambition_s wage_growth_ambition_s)

drop if _merge==2
drop _merge

replace ambition_type_sep=1 if ambition_type_k_4_s==2 & final_educ_year>=1990 & final_educ_year<=1999
replace ambition_type_sep=2 if ambition_type_k_4_s==4 & final_educ_year>=1990 & final_educ_year<=1999
replace ambition_type_sep=3 if ambition_type_k_4_s==3 & final_educ_year>=1990 & final_educ_year<=1999
replace ambition_type_sep=4 if ambition_type_k_4_s==1 & final_educ_year>=1990 & final_educ_year<=1999



drop ambition_type_k_4_s

gen wage_start_mean_ambition_s_sec=wage_start_mean_ambition_s
drop wage_start_mean_ambition_s
gen wage_growth_ambition_s_sec=wage_growth_ambition_s
drop wage_growth_ambition_s

*Merge on third decade

merge 1:1 pnr aar using "Results\tab_B3\Scenario_3\sample2_third_decade.dta", keepusing(ambition_type_k_4_s wage_start_mean_ambition_s wage_growth_ambition_s)

drop if _merge==2
drop _merge


replace ambition_type_sep=1 if ambition_type_k_4_s==1 & final_educ_year>=2000
replace ambition_type_sep=2 if ambition_type_k_4_s==3 & final_educ_year>=2000
replace ambition_type_sep=3 if ambition_type_k_4_s==4 & final_educ_year>=2000
replace ambition_type_sep=4 if ambition_type_k_4_s==2 & final_educ_year>=2000



drop ambition_type_k_4_s

gen wage_start_mean_ambition_s_third=wage_start_mean_ambition_s
drop wage_start_mean_ambition_s
gen wage_growth_ambition_s_third=wage_growth_ambition_s
drop wage_growth_ambition_s

**Get partner types

//generate partners' ambition
by couple_id aar, sort: gen am_male = ambition if koen=="1" & relationship==1
by couple_id aar: gen am_female = ambition if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(am_male)
by couple_id aar: replace am_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(am_female)
by couple_id aar: replace am_female = maxeduc
drop maxeduc

by couple_id aar, sort: gen am_sep_male = ambition_type_sep if koen=="1" & relationship==1
by couple_id aar: gen am_sep_female = ambition_type_sep if koen=="2" & relationship==1
by couple_id aar: egen max_am_sep = max(am_sep_male)
by couple_id aar: replace am_sep_male = max_am_sep
drop max_am_sep
by couple_id aar: egen max_am_sep = max(am_sep_female)
by couple_id aar: replace am_sep_female = max_am_sep
drop max_am_sep


**Save**
save "Results\tab_B3\Scenario_3\joining_couples.dta", replace

